package com.esp.app.brightness;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.FrameLayout;

import com.esp.app.widget.R;

public class BrightnessActivity extends Activity implements OnClickListener{
	

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.brightness);
        
        Button button = (Button)findViewById(R.id.BBtn1);
        button.setOnClickListener(this);
        button = (Button)findViewById(R.id.BBtn2);
        button.setOnClickListener(this);
        button = (Button)findViewById(R.id.BBtn3);
        button.setOnClickListener(this);
        button = (Button)findViewById(R.id.BBtn4);
        button.setOnClickListener(this);
        button = (Button)findViewById(R.id.BBtn5);
        button.setOnClickListener(this);
        FrameLayout frameLayout = (FrameLayout) findViewById(R.id.FrameLayout01);
        frameLayout.setClickable(true);
        frameLayout.setOnClickListener(this);
     
    }

	/*Level should be 0~255*/
    public void setBrightness(int level)
    {
    	ContentResolver cr = getContentResolver();
 		Settings.System.putInt(cr, "screen_brightness", level);
 		Window window = getWindow();
 		LayoutParams attributes = window.getAttributes();
 		float flevel = level;
 		attributes.screenBrightness = flevel/255;
 		getWindow().setAttributes(attributes);
 		Intent intent = new Intent();
 		intent.setAction(BrightnessModel.BRIGHTNESS_CHANGED_ACTION);
 		sendBroadcast(intent);
 		finish();
 	
    }

	
    public void onClick(View view)
    {
	    switch (view.getId())
        {
	        case R.id.BBtn1:
	        	setBrightness(1);
		        break;
	        case R.id.BBtn2:
	        	setBrightness(64);
	        	break;
	        case R.id.BBtn3:
	        	setBrightness(128);
	        	break;
	        case R.id.BBtn4:
	        	setBrightness(192);
	        	break;
	        case R.id.BBtn5:
	        	setBrightness(255);
	        	break;
	        case R.id.FrameLayout01:
	        	finish();
	        	break;
        }
	    
    }

	
}